package com.yonyou.pmclouds.personnel.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.ref.itf.RefCodeConst;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceChecker;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.businessobject.util.SortListUtil;
import com.yonyou.pmclouds.personnel.entity.PersonnelSearchConditionVO;
import com.yonyou.pmclouds.personnel.entity.PersonnelVO;
import com.yonyou.pmclouds.personnel.mapper.PersonnelMapper;
import com.yonyou.pmclouds.personnel.rmiitf.PersonnelQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @Author chulf
 * @Date 2020/4/14
 * @Description
 **/
@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass = PersonnelQuery.class)
public class PersonnelQueryImpl implements PersonnelQuery {
    @Autowired
    private PersonnelMapper personnelMapper;

    @Autowired
    private ReferenceChecker referenceChecker;

    @Override
    public PersonnelVO[] queryPersonnelsByPkEtp(String pkEnterprise) {

        PersonnelVO[] personnelVOS = personnelMapper.selectPersonnelVOSByPkEtp(pkEnterprise);

        return personnelVOS;
    }

    @Override
    public PersonnelVO[] queryPersonnelsByPkEtps(String[] pkEnterprise) {

        PersonnelVO[] personnelVOS = personnelMapper.selectPersonnelVOSByPkEtps(pkEnterprise);
        return personnelVOS;
    }

    @Override
    public Set<String> queryEtpsByUserId(String userId) {

        PersonnelVO[] personnelVOS = personnelMapper.selectPersonnelsVOByUserId(userId);

        HashSet<String> etpPks = new HashSet<>();
        if (ArrayUtils.isNotEmpty(personnelVOS)) {
            for (PersonnelVO personnelVO : personnelVOS) {
                if (personnelVO.getIsContacts() == 1){
                    etpPks.add(personnelVO.getPkEnterprise());
                }
            }
        }
        return etpPks;
    }

    @Override
    public PersonnelVO[] queryByUserIds(String[] userIds) {
        return personnelMapper.queryByUserIds(userIds, RuntimeEnvironment.getTenantId());
    }

    @Override
    public boolean queryPersonnelRef(String code, String[] ids) {
        boolean flag = false;
        Integer count = personnelMapper.selectRefByCode(code, ids);
        if (count != null && count > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public boolean queryRefCheck(String id) throws BusinessException {
        boolean flag = referenceChecker.checkReferenced(RefCodeConst.PERSONNEL, new String[]{id});


        return flag;
    }

    @Override
    public PageObject<PersonnelVO> search(PersonnelSearchConditionVO conditionVO) throws BusinessException {
        conditionVO.setTenantId(RuntimeEnvironment.getTenantId());

        PersonnelVO[] personnelVOS = personnelMapper.search(conditionVO);
        if(personnelVOS == null || personnelVOS.length <= 0){
            return new PageObject<>(new PersonnelVO[0], 0, 0);
        }

        int count = personnelMapper.searchCountByCondition(conditionVO);
        List<PersonnelVO> sortPersonList = Arrays.asList(personnelVOS);
        SortListUtil.sort(sortPersonList, PersonnelVO.SNAME, SortListUtil.ASC);

        PersonnelVO[] personnelVOArray = sortPersonList.toArray(new PersonnelVO[]{});
        return new PageObject<>(personnelVOArray, count, conditionVO.getPageNum());
    }
}
